﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using AForge.Video.DirectShow;
using ZXing;
using ZXing.Common;
using ZXing.OneD;
using ZXing.QrCode;

namespace DeviceUnion.QRCode
{
    public partial class QRCodeForm : Form
    {
        private BarcodeWriter writer = null;
        private BarcodeWriter bc = null;

        public QRCodeForm()
        {
            InitializeComponent();
            this.FormClosing += Form1_FormClosing;
            videoSourcePlayer1 = new AForge.Controls.VideoSourcePlayer()
            {
                Dock = DockStyle.Fill,
            };
            this.panel1.Controls.Add(videoSourcePlayer1);

            this.StartPosition = FormStartPosition.CenterScreen;
            var options = new QrCodeEncodingOptions();
            options.Height = pictureBoxQr.Height;
            options.Width = pictureBoxQr.Width;
            options.CharacterSet = "GBK";

            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;

            var ops = new Code128EncodingOptions();
            ops.Height = this.pictureBox1_barCode.Height;
            ops.Width = this.pictureBox1_barCode.Width;
            ops.PureBarcode = false;
            ops.ForceCodesetB = true;
            bc = new BarcodeWriter();
            bc.Format = BarcodeFormat.CODE_128;
            bc.Options = ops;
            this.Load += Form1_Load;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text += DeviceUnion.Program.S1 + DeviceUnion.Program.S2 + DeviceUnion.Program.S3;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (videoSourcePlayer1.IsRunning)
            {
                videoSourcePlayer1.Stop();
            }
        }

        /// <summary>
        /// 二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonQr_Click(object sender, EventArgs e)
        {
            if (textBoxText.Text == string.Empty)
            {
                MessageBox.Show("输入内容不能为空！");
                return;
            }
            try
            {
                Bitmap bitmap = writer.Write(textBoxText.Text);
                pictureBoxQr.Image = bitmap;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 条形码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxText.Text == string.Empty)
            {
                MessageBox.Show("输入内容不能为空！");
                return;
            }
            try
            {
                Bitmap bitmap = bc.Write(textBoxText.Text);
                pictureBox1_barCode.Image = bitmap;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Decode(Image img)
        {
            try
            {
                Bitmap map = new Bitmap(img);
                BitmapSource source;
                using (MemoryStream ms = new MemoryStream())
                {
                    map.Save(ms, ImageFormat.Png);
                    source = BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                }
                var source2 = new BitmapSourceLuminanceSource(source);
                GlobalHistogramBinarizer binarizer = new GlobalHistogramBinarizer(source2);
                BinaryBitmap binary = new BinaryBitmap(binarizer);
                var reader = new QRCodeReader();
                var res = reader.decode(binary);
                if (reader != null)
                {
                    this.textBox1.Text = res.Text;
                }
                else
                {
                    this.textBox1.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var img = this.pictureBox1.Image;
            Decode(img);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                var f = ofd.FileName;
                if (System.IO.File.Exists(f))
                {
                    this.pictureBox1.Image = Image.FromFile(f);
                }
            }
        }

        private VideoCaptureDevice videoSource = null;
        private AForge.Controls.VideoSourcePlayer videoSourcePlayer1;

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_open_Click(object sender, EventArgs e)
        {
            var guid = FilterCategory.VideoInputDevice.ToString();
            var gid = new Guid(guid);
            var videoDevices = new FilterInfoCollection(gid);
            var selectedDeviceIndex = 0;
            videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);//连接摄像头。
            videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex];
            videoSourcePlayer1.VideoSource = videoSource;
            // set NewFrame event handler
            videoSourcePlayer1.Start();
        }

        private void button5_close_Click(object sender, EventArgs e)
        {
            if (videoSourcePlayer1.IsRunning)
            {
                videoSourcePlayer1.Stop();
            }
        }

        private void button6_p_Click(object sender, EventArgs e)
        {
            if (videoSourcePlayer1.IsRunning)
            {
                var img = videoSourcePlayer1.GetCurrentVideoFrame();
                this.pictureBox1.Image = img;
            }
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            var f = new VLC.VLC_CaptureDesktop();
            f.ShowDialog();
            f.Dispose();
        }

        private void Button5_Click_1(object sender, EventArgs e)
        {
            var f = new FFmpeg.WinForm.FFmpegCapture();
            f.ShowDialog();
            f.Dispose();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var f = new Voice.VoiceTestForm();
            f.ShowDialog();
            f.Dispose();
        }
    }
}